package com.xin.springclouduser.controller;

import com.xin.springclouduser.converter.UserForm2UserDTO;
import com.xin.springclouduser.dataobject.User;
import com.xin.springclouduser.dto.PageResult;
import com.xin.springclouduser.dto.UserDTO;
import com.xin.springclouduser.form.UserForm;
import com.xin.springclouduser.service.UserService;
import com.xin.springclouduser.utils.ResultVOUtils;
import com.xin.springclouduser.vo.ResultVO;
import jdk.nashorn.internal.objects.annotations.Getter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;


@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private RestTemplate restTemplate;

    /**
     * 创建用户(openId需要在参数中传递进来)
     * @param userForm
     * @return
     */
    @PostMapping("/create")  //required 是否为必传
    public ResultVO create(@RequestBody(required = false)UserForm userForm){
        UserDTO convert = UserForm2UserDTO.convert(userForm);
        UserDTO userDTO = userService.create(convert);
        if (ObjectUtils.isEmpty(userDTO)){
            ResultVO error = ResultVOUtils.error(1, "创建失败!");
            return error;
        }
        return ResultVOUtils.success(userDTO);
    }

    /**
     * 更新用户
     * @param userForm
     * @return
     */
    @PostMapping("/update")
    public ResultVO update(@RequestBody(required = false) UserForm userForm){
        UserDTO userDTO = UserForm2UserDTO.convert(userForm);
        UserDTO update = userService.update(userDTO);
        if (ObjectUtils.isEmpty(update)){
              return ResultVOUtils.error(1,"更新用户失败!");
        }
        return ResultVOUtils.success(update);
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @GetMapping("/delete")
    public ResultVO delete(@RequestParam(name = "uid",defaultValue = "") String uid){
        boolean delete = userService.delete(uid);
        if (!delete){
            return ResultVOUtils.error(1,"删除失败!");
        }
        return ResultVOUtils.success();
    }

    /**
     * 根据用户Id查询用户
     * @param uid
     * @return
     */
    @GetMapping("findOneByUserId")
    public ResultVO findOneByUserId(@RequestParam(name = "uid",defaultValue = "") String uid){
        UserDTO userDTO = userService.findOneByUserId(uid);
        if (ObjectUtils.isEmpty(userDTO)){
            return ResultVOUtils.error(1,"用户不存在!");
        }
        return ResultVOUtils.success(userDTO);
    }

    /**
     * 根据openId查询用户
     * @param openId
     * @return
     */
    @GetMapping("/findOneByOpenId")
    public ResultVO findOneByOpenId(@RequestParam(name = "openId",defaultValue = "") String openId){
        UserDTO userDTO = userService.findOneByOpenId(openId);
        if (ObjectUtils.isEmpty(userDTO)){
            return ResultVOUtils.error(1,"用户不存在!");
        }
        return ResultVOUtils.success(userDTO);
    }

    /**
     * 查询全部用户
     * @param pageable
     * @return
     */
    @GetMapping("/findAll")
    public ResultVO findAll(@PageableDefault(page = 1,size = 10) Pageable pageable){
        Sort sort=new Sort(Sort.Direction.DESC,"createTime");
        PageRequest pageRequest=new PageRequest(pageable.getPageNumber()-1,pageable.getPageSize(),sort);
        PageResult<User> result = userService.findAll(pageRequest);
        return ResultVOUtils.success(result);
    }
}
